{
 "cells": [
  {
   "cell_type": "code",
   "id": "e1f27dac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T10:38:05.936054Z",
     "start_time": "2025-08-08T10:38:04.626916Z"
    }
   },
   "source": [
    "from langchain.vectorstores import Milvus\n",
    "from langchain.embeddings import OllamaEmbeddings\n",
    "from langchain.llms import Ollama\n",
    "from langchain.chains import RetrievalQA"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "id": "35aebec7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-08T10:40:48.909928Z",
     "start_time": "2025-08-08T10:40:00.303863Z"
    }
   },
   "source": [
    "\n",
    "# 连接Milvus和本地模型\n",
    "embeddings = OllamaEmbeddings(model=\"nomic-embed-text\")\n",
    "vector_db = Milvus(\n",
    "    embeddings,\n",
    "    connection_args={\"host\": \"localhost\", \"port\": \"19530\"},  # Milvus默认端口\n",
    "    collection_name=\"my_docs\"\n",
    ")\n",
    "llm = Ollama(model=\"llama2\")\n",
    "\n",
    "# 创建RAG链\n",
    "qa_chain = RetrievalQA.from_chain_type(\n",
    "    llm=llm,\n",
    "    chain_type=\"stuff\",\n",
    "    retriever=vector_db.as_retriever()\n",
    ")\n",
    "\n",
    "# 提问\n",
    "print(qa_chain.run(\"文档中关于XX的内容是什么？\"))"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_16932\\1337300291.py:8: LangChainDeprecationWarning: The class `Ollama` was deprecated in LangChain 0.3.1 and will be removed in 1.0.0. An updated version of the class exists in the :class:`~langchain-ollama package and should be used instead. To use it run `pip install -U :class:`~langchain-ollama` and import as `from :class:`~langchain_ollama import OllamaLLM``.\n",
      "  llm = Ollama(model=\"llama2\")\n",
      "C:\\Users\\PC\\AppData\\Local\\Temp\\ipykernel_16932\\1337300291.py:18: LangChainDeprecationWarning: The method `Chain.run` was deprecated in langchain 0.1.0 and will be removed in 1.0. Use :meth:`~invoke` instead.\n",
      "  print(qa_chain.run(\"文档中关于XX的内容是什么？\"))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Of course! I'd be happy to help you with your question. Can you please provide the context or document that you are referring to, so I can better understand what you are asking about?\n"
     ]
    }
   ],
   "execution_count": 3
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
